//
//  FANEmoticonStackView.m
//  Weibo - OC
//
//  Created by user on 16/4/27.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANEmoticonStackView.h"

@interface FANEmoticonStackView ()

@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation FANEmoticonStackView

- (void)setSelectedSection:(int)selectedSection{
    
    _selectedSection = selectedSection;
    
//    NSLog(@"%d",selectedSection);
    
    UIButton *button = [self viewWithTag:selectedSection];
    
    if (button.tag != self.selectedButton.tag) {
        
        button.selected = true;
        
        self.selectedButton.selected = false;
        
        self.selectedButton = button;
        
    }
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
        self.distribution = UIStackViewDistributionFillEqually;
    }
    
    return self;
}

- (void)setupUI{
    
    self.tag = 20000;
    
    [self addEmoticonButton:@"compose_emotion_table_left" andTitle:@"最近" and:Recent];
    [self addEmoticonButton:@"compose_emotion_table_mid" andTitle:@"默认" and:Default];
    [self addEmoticonButton:@"compose_emotion_table_mid" andTitle:@"Emoji"and:Emoji];
    [self addEmoticonButton:@"compose_emotion_table_right" andTitle:@"浪小花"and:Lxh];

    
}

- (void)addEmoticonButton:(NSString *)imageName andTitle:(NSString *)title and:(FANEmoticonStackType)type{
    
    UIButton *button = [[UIButton alloc] init];
    
    button.tag = type;
    
    [button setTitle:title forState:UIControlStateNormal];
    
    if ([title isEqualToString:@"最近"]) {
        
        button.selected = true;
        
        self.selectedButton = button;
    }
    
    [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateSelected];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_normal",imageName]] forState:UIControlStateNormal];
    
    [button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_selected",imageName]] forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(didEmoticonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addArrangedSubview:button];
}

- (void)didEmoticonClick:(UIButton *)button{
    
    if (self.selectedButton == button) {
        
        return;
    }
    
    self.stackBlock(button.tag);

    self.selectedButton.selected = false;
    
    button.selected = true;
    
    self.selectedButton = button;
    
}

@end
